﻿//若要启用该调用，请在 ScheduledAgent.cs 文件的顶部放置以下代码行。
#define DEBUG_AGENT

using System;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;


namespace CUWo.PushTaskAgent
{
    public class ScheduledAgent : ScheduledTaskAgent
    {
        /// <remarks>
        /// ScheduledAgent 构造函数，初始化 UnhandledException 处理程序
        /// </remarks>
        static ScheduledAgent()
        {
            // 订阅托管的异常处理程序
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                Application.Current.UnhandledException += UnhandledException;
            });
        }

        /// 出现未处理的异常时执行的代码
        private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                // 出现未处理的异常；强行进入调试器
                Debugger.Break();
            }
        }

        /// <summary>
        /// 运行计划任务的代理
        /// </summary>
        /// <param name="task">
        /// 调用的任务
        /// </param>
        /// <remarks>
        /// 调用定期或资源密集型任务时调用此方法
        /// </remarks>
        protected override void OnInvoke(ScheduledTask task)
        {
            //TODO: Add code to perform your task in background
            string toastMessage = "获取的信息内容";

            // If your application uses both PeriodicTask and ResourceIntensiveTask
            // you can branch your application code here. Otherwise, you don't need to.
            if (task is PeriodicTask)
            {
                // Execute periodic task actions here.
                toastMessage = "Periodic task running.";
            }
            else
            {
                // Execute resource-intensive task actions here.
                toastMessage = "Resource-intensive task running.";
            }


            // Launch a toast to show that the agent is running.
            // The toast will not be shown if the foreground application is running.
            ShellToast toast = new ShellToast();
            toast.Title = "Background Agent Sample";
            toast.Content = toastMessage;
            toast.NavigationUri = new Uri("/StartPage.xaml", UriKind.Relative);
            toast.Show();

            // If debugging is enabled, launch the agent again in one minute.
#if DEBUG_AGENT
            //#define DEBUG_AGENT
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
#endif

            // Call NotifyComplete to let the system know the agent is done working.
            NotifyComplete();

//            //Toast通知
//            ShellToast toast = new ShellToast();
//            toast.Title = "Background Agent";
//            toast.Content = "Periodic task running";
//            toast.NavigationUri = new Uri("StartPage.xaml", UriKind.Relative);
//            toast.Show();

//            IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("text.txt", FileMode.Open, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication());
//            StreamWriter writer = new StreamWriter(fileStream);
//            writer.WriteLine(string.Format("{0}", 100));
//            writer.Close();

//#if DEBUG_AGENT
//            //#define DEBUG_AGENT
//            //ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));
//#endif

//            NotifyComplete();
        }
    }
}